Deklaration eines Interfaces mit Methoden-Prototypen

Syntax
INTERFACE name_1 ( * optional_begin *)  EXTENDS interface_2, interface_3, ... interface_n (* optional_end *)
 
(* optional: declarations of method prototypes *)
 
END_INTERFACE
Bedeutung

Deklaration eines →Interfaces (ein Feature der →objektorientierte Programmierung) , wobei name_1 ein →IEC-Bezeichner sein muss.
Die Deklaration ist in einem ST-Objekt möglich – darin erfolgt die Deklaration entweder im globalen →Namespace oder in einem deklarierten Namespace. INTERFACE und END_INTERFACE sind die →Schlüsselwörter für die Deklaration der Schnittstelle.

(Info) In der Objektorientierten Programmierung trennen Sie mit Hilfe eines Interfaces die Interface-Spezifikation von ihrer Implementierung als eine →Klasse. Das erlaubt unterschiedliche Implementierungen einer gemeinsamen Interface-Spezifikation.

Das Schlüsselwort EXTENDS ist optional. Geben Sie EXTENDS an, falls Sie das Interface von anderen Interfaces ableiten wollen (auch als Basis-Interfaces bekannt). Die Namen dieser Basis-Interfaces geben Sie hinter EXTENDS an (durch einen Beistrich voneinander getrennt).
Die Namen der Methoden-Prototypen im Interface und in den Basis-Interfaces müssen eindeutig sein. Mehrfach vorhandene Methoden-Prototypen aus dem gleichem Interface werden jedoch ignoriert (siehe Methoden-Prototyp UP im folgenden Beispiel 2).

Gut zu wissen

(Graue Glühbirne) In einem Interface dürfen Sie nur →Methoden-Prototypen deklarieren. Siehe folgender Abschnitt "Deklaration eines Methoden-Prototyp".

(Graue Glühbirne) Ein Interface kann als Typ für eine Variable verwendet werden (siehe "Deklaration von Variablen, die auf einem Interface basieren"). Zusätzlich kann ein Funktionsbaustein oder eine →Klasse die Interfaces implementieren (siehe "Deklaration eines Funktionsbausteins in ST").

(Graue Glühbirne) Ein Interface verhält sich wie ein →Funktionsbaustein.

Deklaration eines Methoden-Prototyp

Ein Methoden-Prototyp ist eine eingeschränkte Deklaration einer →Methode (mit dem impliziten Modifizier PUBLIC) zur Verwendung bei einem Interface.

Übersicht zur Syntax
METHOD name_1 (* optional_begin *) : type (* optional_end *) 
 
(* optional: declaration of variables but only input variables, output variables and in-out variables are allowed in method prototypes *)
 
END_METHOD

Im Unterschied zur Methode (siehe "Deklaration einer Methode" für Details) darf ein Methoden-Prototyp nur diese Elemente enthalten:

Beachten Sie, dass Sie bei einem Methoden-Prototyp keine Schlüsselwörter (z.B. Modifizierer) und auch keinen Body (also Code zum Ausführen) definieren, wie dies bei einer Methode möglich ist.

Beispiel 1: Interface mit 2 Methoden-Prototypen

INTERFACE COUNT
  METHOD UP : UINT
    VAR_INPUT
      INC: UINT;
    END_VAR
    VAR_OUTPUT
      QU: BOOL;
    END_VAR
  END_METHOD
 
  METHOD UP5 : UINT
    VAR_OUTPUT
      QU: BOOL;
    END_VAR
  END_METHOD
END_INTERFACE
Beispiel 2: Wie Beispiel 1 mit abgeleiteten Interfaces
INTERFACE COUNT_Original
  (* "COUNT_Original" contains the method prototypes "UP" und "UP5". *)
 
  METHOD UP : UINT
    VAR_INPUT
      INC: UINT;
    END_VAR
    VAR_OUTPUT
      QU: BOOL;
    END_VAR
  END_METHOD
 
  METHOD UP5 : UINT
    VAR_OUTPUT
      QU: BOOL;
    END_VAR
  END_METHOD
END_INTERFACE
 
INTERFACE COUNT_BASE
  (* "COUNT_BASE" contains the method prototype "UP". *)
 
  METHOD UP : UINT
    VAR_INPUT
      INC: UINT;
    END_VAR
    VAR_OUTPUT
      QU: BOOL;
    END_VAR
  END_METHOD
 
END_INTERFACE
 
INTERFACE COUNT EXTENDS COUNT_BASE
  (* "COUNT" contains the method prototype "UP5". Due to "EXTENDS COUNT_BASE", there is also the method prototype "UP". *)
 
  METHOD UP5 : UINT
    VAR_OUTPUT
      QU: BOOL;
    END_VAR
  END_METHOD
END_INTERFACE
 
INTERFACE COUNT3 EXTENDS COUNT_BASE, COUNT_Original
  (* "COUNT3" contains the method prototype "UP10". Due to "EXTENDS COUNT_BASE, COUNT_Original", there are also the method prototypes "UP5" und "UP". *)
  (* Here an error is reported. Reason: "UP" exists multiple times in the different interfaces "COUNT_BASE" und "COUNT_Original". *)
  
  METHOD UP10 : UINT
  END_METHOD
END_INTERFACE
 
INTERFACE Sample EXTENDS COUNT_BASE, COUNT_BASE
  (* Due to "EXTENDS COUNT_BASE, COUNT_Original", there is also the method prototype "UP". *)
  (* This is OK although "UP" actually exists multiple times. The 2nd occurrence of "UP" in "COUNT_BASE" is ignored because "UP" is derived from the same interface "COUNT_BASE". *) 
END_INTERFACE
 
INTERFACE COUNT2 EXTENDS COUNT_BASE, COUNT
  (* "COUNT2" contains the method prototype "UP10". Due to "EXTENDS COUNT_BASE, COUNT", there are also the method prototypes "UP5" und "UP". *)   
  (* This is OK although "UP" actually exists multiple times. The 2nd occurrence of "UP" in "COUNT" is ignored because "UP" is derived from the same interface "COUNT_BASE". Background: The interface "COUNT" is derived from the interface "COUNT_BASE" as well. *) 
  METHOD UP10 : UINT
  END_METHOD  
END_INTERFACE

Beispiel 3

INTERFACE I_Motor
  /*
   * Returns the current speed of the motor in RPM.
   */
  METHOD GetCurrentSpeed : UINT
  END_METHOD
  
  /*
   * Sets the desired speed of the motor.
   * The desired ramp-up time can be specified by an input parameter.
   * Returns the current motor speed as an output parameter.
   */
  METHOD SetSpeed
    VAR_INPUT
      i_RampUpTime : TIME := T#1s;
      i_DesiredSpeed : UINT;
    END_VAR
    VAR_OUTPUT
      o_CurrentSpeed : UINT;
    END_VAR
  END_METHOD
END_INTERFACE

Weitere Beispiele für Interfaces finden Sie unter "Beispiele für Verwendung von Interfaces und Variablen basierend auf diesen Interfaces (inkl. Zuweisungen)".